package color;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * 调色的滑动面板
 */
public class LittleUI extends JDialog {
    private static final long serialVersionUID = 1L;
    //确认按钮
    private JButton okBtn;
    //亮度块
    private JLabel bLabel;
    //对比度块
    private JLabel cLabel;
    //饱和度块
    private JLabel sLabel;
    //亮度滑动条
    private JSlider bSlider;
    //对比度滑动条
    private JSlider cSlider;
    //饱和度滑动条
    private JSlider sSlider;

    /**
     * 构造方法
     * @param parent 父窗口
     */
    public LittleUI(JFrame parent){
        super(parent,"调整图像亮度、对比度、饱和度");
        initComponent();

    }

    /**
     * 面板四个滑块
     */
    public void initComponent(){
        okBtn = new JButton("确定");
        bLabel = new JLabel("亮度");
        cLabel = new JLabel("对比度");
        sLabel = new JLabel("饱和度");
        //滑块最大值最小值与初始值
        bSlider = new JSlider(JSlider.HORIZONTAL,-100,100,0);
        //主刻度标记之间间隔
        bSlider.setMajorTickSpacing(40);
        //次刻度标记之间间隔
        bSlider.setMinorTickSpacing(10);
        //确定是否绘制标签
        bSlider.setPaintLabels(true);
        //确定是否在滑块上绘制刻度标记
        bSlider.setPaintTicks(true);
        //确定是否在滑道上绘制滑道
        bSlider.setPaintTrack(true);
        //对比度同理
        cSlider = new JSlider(JSlider.HORIZONTAL,-100,100,0);
        cSlider.setMajorTickSpacing(40);
        cSlider.setMinorTickSpacing(10);
        cSlider.setPaintTicks(true);
        cSlider.setPaintLabels(true);
        cSlider.setPaintTrack(true);
        //饱和度同理
        sSlider = new JSlider(JSlider.HORIZONTAL,-100,100,0);
        sSlider.setMajorTickSpacing(40);
        sSlider.setMinorTickSpacing(10);
        sSlider.setPaintTicks(true);
        sSlider.setPaintLabels(true);
        sSlider.setPaintTrack(true);
        //设置面板
        this.getContentPane().setLayout(new BorderLayout());
        //建立新Jpanel
        JPanel bPanel = new JPanel();
        //亮度添加
        bPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        bPanel.add(bLabel);
        bPanel.add(bSlider);
        //对比度添加
        JPanel cPanel = new JPanel();
        cPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        cPanel.add(cLabel);
        cPanel.add(cSlider);
        //饱和度添加
        JPanel sPanel = new JPanel();
        sPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        sPanel.add(sLabel);
        sPanel.add(sSlider);
        //新Jpanel
        JPanel contentPanel = new JPanel();
        //设置大小
        contentPanel.setLayout(new GridLayout(3,1));
        //添加进面板
        contentPanel.add(bPanel);
        contentPanel.add(cPanel);
        contentPanel.add(sPanel);
        //按钮设置
        JPanel btnPanel = new JPanel();
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        //添加
        btnPanel.add(okBtn);
        //布局
        this.getContentPane().add(contentPanel,BorderLayout.CENTER);
        this.getContentPane().add(btnPanel,BorderLayout.SOUTH);
        this.pack();

    }

    /**
     *调整窗口位置
     * @param w 窗口
     */
    public static void centre(Window w){
        Dimension us = w.getSize();
        Dimension them = Toolkit.getDefaultToolkit().getScreenSize();
        int newX = (them.width-us.width)/2;
        int newY = (them.height-us.height)/2;
        //设置
        w.setLocation(newX,newY);

    }

    /**
     * 返回滑块的当前亮度值
     * @return 亮度
     */
    public int getBright(){
        return bSlider.getValue();
    }

    /**
     * 返回滑块的当前对比度
     * @return 对比度
     */
    public int getContrast(){
        return cSlider.getValue();
    }

    /**
     * 返回滑块的当前饱和度
     * @return 饱和度
     */
    public int getSat(){
        return sSlider.getValue();
    }

    /**
     * 显示窗口
     */
    public void showUI(){
        centre(this);
        this.setVisible(true);
    }

    /**
     * 设置监听器
     * @param l 事件
     */
    public void setActionListener(ActionListener l){
        this.okBtn.addActionListener(l);
    }
}

